<?php
namespace catchAdmin\product\model;

use catchAdmin\product\model\search\PackSearch;
use catcher\base\CatchModel;
use think\Exception;
use think\facade\Db;

class Pack extends CatchModel
{

    use PackSearch;
    
    protected $name = 'db_packing';
    protected $field = [
            'id', // 
			'packing_id', // 包装编号
			'packing_num', // 包装数量
            'barcode', // 包装数量
			'unit_name', // 单位名称
			'unit_id', // 单位id
			'describe', // 包装名称
			'tenant_id', // 商户id
            'creator_id', // 创建者ID
			'created_at', // 创建时间
			'updated_at', // 更新时间
			'deleted_at', // 删除状态，0未删除 >0 已删除
            'serial_number',
            'layup_num'//单位托盘可放置的数量
    ];
    /**
     * 列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        $pack =  $this->withoutField(['updated_at'], true)
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate();

        foreach ($pack as &$value){
            $value['_packing_hierarchy'] = "托/" . $value['unit_name'] . "/件";
            $value['_packing_num'] = "1/" . $value['layup_num'] . "/" . $value['layup_num'] * $value['packing_num'];
            $value['_packing_min_unit'] = $value['packing_num'];
        }

        return $pack;
    }
    /**
     *  无分页列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getAttrList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this
            ->catchSearch()
            ->field('id,packing_id,packing_num,unit_name,layup_num')
            ->fieldRaw('layup_num * packing_num as packing_total')
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->select();
    }

    public static function onBeforeInsert($packing){
        //查询数量
        $count =  Db::table('db_packing')->where('tenant_id',$packing->tenant_id)->count();
        //公司代码
        $co_code = Db::table('users')->where('id',$packing->tenant_id)->value('co_code');

        if( !empty($co_code) ){
            $head = strtoupper($co_code)  . "P";
            $packing->serial_number = $head . str_pad($count + 1,5,'0',STR_PAD_LEFT);
        }else{//无锡公司
            $packing->serial_number = "P" . str_pad($count + 1,5,'0',STR_PAD_LEFT);
        }
    }

    //通过商品，规格，等到箱数，托盘数量
    public function getTrayBoxNum($number,$packing_id){
        $packing = $this->where(['packing_id'=>$packing_id])->find();

        if(empty($packing['packing_num']) || empty($packing['layup_num'])){
           throw new Exception('商品层级错误，未定义托盘可放置数量或包装数量');
        }

        $box_num = ceil($number / $packing['packing_num']);
        $tray_num = ceil($box_num / $packing['layup_num']);

        return [
            'box_num'=>$box_num,
            'tray_num'=>$tray_num
        ];
    }
}
